package com.tanhua.admin.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tan.model.domain.UserInfo;
import com.tan.model.mongo.Movement;
import com.tan.model.vo.MovementsVo;
import com.tan.model.vo.MovementsadminVo;
import com.tan.model.vo.PageResult;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.MoventApi;
import com.tanhua.dubbo.api.UserInfoapi;
import com.tanhua.dubbo.api.VideoApi;;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ManagerService {
    @DubboReference
     UserInfoapi userInfoapi;
    @DubboReference
    VideoApi videoApi;
    @DubboReference
    MoventApi moventApi;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    public PageResult findAllUsers(Integer page, Integer pagesize) {
        IPage<UserInfo> pageResult = userInfoapi.findAll(page, pagesize);
        List<UserInfo> userInfoList=pageResult.getRecords();
        for (UserInfo userInfo : userInfoList) {
            Long userId = userInfo.getId();
            if(redisTemplate.hasKey(Constants.USER_FREEZE+userId)){
                userInfo.setUserStatus("2");
            }
        }
        PageResult pageResultture = new PageResult(page,pagesize,userInfoList.size(),userInfoList);
        return pageResultture;

    }

    public ResponseEntity findById(Long userId) {
      UserInfo userInfo =  userInfoapi.getUserInfo(userId);
     String key=Constants.USER_FREEZE+userId;
     if(redisTemplate.hasKey(key)){
         userInfo.setUserStatus("2");
     }
      return ResponseEntity.ok(userInfo);
    }

    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {
        PageResult pageResult= videoApi. findByUserId(page, pagesize, uid);
        return pageResult;
    }

    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
     List<Movement> Movements =  moventApi.findByUserId(uid, state, page, pagesize);
     List<MovementsVo>movementsVos = new ArrayList<>();
     for (Movement movement : Movements) {
        UserInfo userInfo = userInfoapi.getUserInfo( movement.getUserId());
        MovementsVo movementsVo = MovementsVo.init(userInfo, movement);
        movementsVos.add(movementsVo);
     }
     PageResult pageResult = new PageResult(page,pagesize,movementsVos.size(),movementsVos);
      return pageResult;
    }

    public ResponseEntity messages(String id) {
        Movement movement = moventApi.getMovement(id);
        UserInfo userInfo = userInfoapi.getUserInfo(movement.getUserId());
        MovementsVo movementsVo = MovementsVo.init(userInfo, movement);
        MovementsadminVo movementsadminVo = MovementsadminVo.init(movementsVo);
        return ResponseEntity.ok(movementsadminVo);
    }

    public Map userFreeze(Map params) {
       Integer userId = (Integer) params.get("userId");
        Integer freezingTime= Integer.parseInt(params.get("freezingTime").toString());//冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
        int day=0;
        if (freezingTime==1)
            day=3;
        if (freezingTime==2)
            day=7;

        String Key= Constants.USER_FREEZE+userId;
       String value= JSON.toJSONString(params);
       if(day>0){
        redisTemplate.opsForValue().set(Key,value,day, TimeUnit.MINUTES);
       }else {
           redisTemplate.opsForValue().set(Key,value);
       }
       Map map = new HashMap();
       map.put("message","冻结成功");
       return map;
    }

    public Map userUnfreeze(Map params) {
        Integer userId = (Integer) params.get("userId");
        String Key= Constants.USER_FREEZE+userId;
        redisTemplate.delete(Key);
        Map map = new HashMap();
        map.put("message","解冻成功");
        return map;
    }
}
